package com.lixixi.tingshu.common.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Component
public class TokenDeliverInterceptor implements RequestInterceptor {
	@Override
	public void apply(RequestTemplate requestTemplate) {
		//获取当前线程内的请求上下文
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		//获取当前请求中携带的token
		String token = request.getHeader("token");
		if (StringUtils.isNotBlank(token))
		{
			//如果token存在
			//为下一次feign接口调用添加token
			requestTemplate.header("token", token);
		}
	}
}
